#pragma once

#include "tcservercore_global.h"

#include "SignatureGenerator.h"
#include <QMap>
#include <AliOSSAdapter.h>

namespace TCServer
{
    namespace Manager
    {
        class TCSERVERCORE_EXPORT FileManager
        {
        public:
			struct PrepareResult {
                bool success;
                QString path;
				QString datetimestr;
				QString signature;
				QString contentType;
				QMap<QString, QByteArray> ossHeaders;
            };
        private:
            FileManager();
            ~FileManager() {}
        public:
            PrepareResult prepareUpload( const QString &ip, quint64 userId, const QString &md5inbase64, const QString &suffix);
			void removeUrlAsync(const QString &url);
            bool isTempUploadsUrl(const QString &url);
//            void markUsed(const QString &url);
            void copyUrlAsync(const QString &urlSource, const QString &pathDest);
            void moveUrlAsync(const QString &urlSource, const QString &pathDest);
		private:
			QString url2path(const QString &url);
        public:
            static FileManager* instance();
		private:
			QMap<QString, QString> suffixes = {
				{ "flac", "application/octet-stream" },
				{ "ape", "application/octet-stream" },
				{ "wav", "audio/x-wav" },
				{ "mp3", "audio/mpeg" },
				{ "ogg", "audio/ogg" },
				{ "gif", "image/gif" },
				{ "jpg", "image/jpeg" },
				{ "jpeg", "image/jpeg" },
				{ "png", "image/png" },
				{ "bmp", "image/bmp" },
				{ "txt", "text/plain" },
				{ "html", "text/html" },
			};
            QString tempUploadsDir = "temp";

            // A user signature can only upload files
            AliOSS::SignatureGenerator userKeyGen = AliOSS::SignatureGenerator(
                "tcres",
                "sSot2oqZBG1xTcJs",
				"iwVz8BO5ewe1X1JC1CbMZTQNtdqQB6");

            // A admin signature can do anything to OSS
			AliOSS::SignatureGenerator adminKeyGen = AliOSS::SignatureGenerator(
				"tcres",
				"L9LT1rmDHa7P6HTF",
				"RC6cgjXoMiMHNjUwELKgYBEYdmnfnf");
			AliOSS::AliOSSAdapter adminAdapter;
        };

    }
}